package com.itboye.ihomebank.adapter;

import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.bean.ChanKanWeiXiuBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.bean.WoDeQiuZuBean;
import com.itboye.ihomebank.presenter.RepairPersenter;
import com.itboye.ihomebank.support.commonadapter.MutiplyCommonAdapter;
import com.itboye.ihomebank.support.commonadapter.ViewHolder;
import com.itboye.ihomebank.util.ByAlert;

/**
 * 我的生活管家（派单中）
 */
public class MyGuanJiaAdapter01 extends MutiplyCommonAdapter<ChanKanWeiXiuBean>
		implements Observer {
	List<ChanKanWeiXiuBean> beans;

	RepairPersenter repairPersenter;
	
	RepairPersenter repairPersenter02;

	Context context;
	int position;
	String id;
	String id02;

	public MyGuanJiaAdapter01(Context context, List<ChanKanWeiXiuBean> datas,
			int... layoutId) {
		super(context, datas, layoutId);
		beans = datas;
		repairPersenter = new RepairPersenter(this);
		repairPersenter02 = new RepairPersenter(this);
	}

	@Override
	public int getItemViewType(int position) {
		return 0;
	}

	@Override
	public void getItemPosition(int position) {
		this.position = position;
	}

	@Override
	public void convert(ViewHolder holder, final ChanKanWeiXiuBean t, int type,
			int position) {
		holder.setText(R.id.guanjia_danhao, t.getId());
		holder.setText(R.id.guanjia_leixing, t.getRepairstatus());
		holder.setText(R.id.guanjia_address, t.getAddress());
		holder.setText(R.id.guanjia_name, t.getUname());

		holder.setText(R.id.guanjia_01, "师傅已到");
		holder.setText(R.id.guanjia_02, "取消申请");
		holder.setVisible(R.id.guanjia_03, false);
		//取消申请
		holder.setOnClickListener(R.id.guanjia_02, position,
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						id=t.getId();
						repairPersenter.shenQingWeiXiu("", "", "", "", "", "",
								"", "", "", "2", t.getId());

					}
				});
		//师傅已到(此订单不可选师傅已到)
		holder.setOnClickListener(R.id.guanjia_01, position, new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				id02=t.getId();
				repairPersenter02.shiFuYiDao(t.getId());
			}
		});
	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		// ByAlert.alert(entity.getData());
		if (entity != null) {
			if (!entity.getCode().equals(0)) {

				if (entity.getEventType() == RepairPersenter.shenQingWeiXiu_success) {
					ByAlert.alert(entity.getData());
					Iterator<ChanKanWeiXiuBean> iterator = beans.iterator();
					while (iterator.hasNext()) {
						ChanKanWeiXiuBean bean = iterator.next();
						if (id.equals(bean.getId())) {
							iterator.remove();
							notifyDataSetChanged();
							break;
						}
					}
				}
				if (entity.getEventType() == RepairPersenter.shenQingWeiXiu_fail) {
					ByAlert.alert(entity.getData());
				}
				
				if (entity.getEventType() == RepairPersenter.yiDao_success) {
					ByAlert.alert(entity.getData());
					
					Iterator<ChanKanWeiXiuBean> iterator = beans.iterator();
					while (iterator.hasNext()) {
						ChanKanWeiXiuBean bean = iterator.next();
						if (id02.equals(bean.getId())) {
							iterator.remove();
							notifyDataSetChanged();
							break;
						}
					}
					
				}
				if (entity.getEventType() == RepairPersenter.yiDao_fail) {
					ByAlert.alert(entity.getData());
				}
			}
		}
	}

}
